import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode :
 * @author: WXY
 * @create: 2022-12-25 15:50
 * @Version 1.0
 **/

public class offer_eight3_reorderList {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    //将链表存入ArrayList中，再从list中取出数据进行操作连接
    public void reorderList(ListNode head) {
        List<ListNode> list = new ArrayList<>();
        ListNode node = head;
        while (node  != null) {
            list.add(node);
            node = node.next;
        }
        int fast = list.size() - 1;
        int slow = 0;
        ListNode pre = null;
        while (fast > slow) {
            ListNode sub = list.get(slow);
            ListNode subFast = list.get(fast);
            sub.next = subFast;
            subFast.next = null;
            if (pre != null) {
                pre.next = sub;
            }
            pre = subFast;
            fast--;
            slow++;
        }
        if (slow == fast) {
            if (pre != null) {
                pre.next = list.get(slow);
                pre.next.next = null;
            }
        }
    }
}
